/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dal;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Hailk_c00639
 */
public class MyConnection {

    private Connection con;
    private ResultSet rs;
    private PreparedStatement pst;
    private CallableStatement cst;
    private Statement st;

    public Connection getConnection() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;instanceName=SQLEXPRESS;databaseName=ProjectG3SemIV", "sa", "1234$");

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MyConnection.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(MyConnection.class.getName()).log(Level.SEVERE, null, ex);
        }

        return con;
    }
    
    public void closeDB()
    {
        try
        {
            if(!this.con.isClosed())
            {
                this.con.close();
            }
        }catch(Exception e)
        {
            System.out.println(e.toString());
        }
    }
    
    public ResultSet executeQuery(String query) {
        try {
            cst = this.getConnection().prepareCall(query);
            rs = cst.executeQuery();
            return rs;
        } catch (Exception ex) {
          
        }
        return null;
    }
    
    public ResultSet executeQuery(String query, Object[] objects) {
        try {
            cst = this.getConnection().prepareCall(query);
            for (int i = 0; i < objects.length; i++) {
                cst.setObject(i + 1, objects[i]);
            }
            
            return cst.executeQuery();
        } catch (Exception ex) {
        }
        return null;
    }
    
    public boolean executeUpdate(String query, Object[] objects) {
        try {
            cst = this.getConnection().prepareCall(query);
            for (int i = 0; i < objects.length; i++) {
                cst.setObject(i + 1, objects[i]);
            }
            
            int result = cst.executeUpdate();
            this.closeConnection();
            return result > 0;
        } catch (Exception ex) {
        }
        return false;
    }
    
    public ResultSet executeStatement(String query) {
        try {
            st = this.getConnection().createStatement();
            rs = st.executeQuery(query);
            return rs;
        } catch (Exception ex) {
          
        }
        return null;
    }
    
    public void closeConnection() {
        try {
            if (con != null ) {
                con.close();
                con = null;
            }
            
            if (rs != null) {
                rs.close();
                rs = null;
            }
            
            st = null;
            cst = null;
            pst = null;
        } catch (Exception ex) {
           
        }
    }
}
